//
//  FSDiscoverController.m
//  fanshu
//
//  Created by mh on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSDiscoverController.h"
#import "FSArticleViewController.h"
#import "FSSpecialTopicViewController.h"
#import "FSSubMitController.h"
#import "FSNavController.h"
#import "FSSearchController.h"

#import "FSTitleView.h"
#import "FSScrollerToolBar.h"
#import "FSScrollerView.h"
@interface FSDiscoverController ()<FSTitleViewDelegate>
@property(nonatomic,weak)FSBaseTableController *currentVc;
@end

@implementation FSDiscoverController
#pragma mark - 懒加载

#pragma  mark -系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航条 按钮
    [self setNavItem];
    //添加子控制器
    [self addchildViewControllers];
    //去除scrollerview的自动调整
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //切换标签时，要不要销毁重做
    //手动选中第一个标签
    FSTitleView *titleView = (FSTitleView *)self.navigationItem.titleView;
     [titleView showSelectedFirst:YES];
    self.currentVc.scrollerToolBar.alpha = 0.0f;
    [UIView animateWithDuration:0.2f animations:^{
        self.currentVc.scrollerToolBar.alpha = 1.0f;
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIView animateWithDuration:0.2f animations:^{
        self.currentVc.scrollerToolBar.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self.currentVc.scrollerToolBar removeFromSuperview];
    }];
}

#pragma mark - 初始化方法
//添加子控制器
- (void)addchildViewControllers{
    // 文章
    FSArticleViewController *vc1 = [[FSArticleViewController alloc] init];
    [self addChildViewController:vc1];
    
    // 专题
    FSSpecialTopicViewController *vc2 = [[FSSpecialTopicViewController alloc] init];
    [self addChildViewController:vc2];
}
    //设置导航条 按钮
- (void)setNavItem{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(clound) image:@"button_submit" highImage:nil];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(search) image:@"icon_search" highImage:nil];

    FSTitleView *titleView = [[FSTitleView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [titleView.firstTitle setTitle:@"文章" forState:UIControlStateNormal];
    [titleView.secondTitle setTitle:@"专题" forState:UIControlStateNormal];
    titleView.delegate = self;
    self.navigationItem.titleView = titleView;
}
#pragma mark -交互方法
//代理 子导航条上按钮被点击
- (void)titleViewOnClick:(FSTitleView *)titleView btn:(UIButton *)btn{
    //根据按钮类型跳转到对应的子控制器
    [self switchView:btn.tag];
}

//搜索按钮点击
- (void)search{
    //model  搜索控制器
    FSSearchController *Vc = [[FSSearchController alloc] init];
    Vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:Vc animated:YES completion:nil];
}
//投稿
- (void)clound{
    //model出   投稿控制器
    FSNavController *nav = [[FSNavController alloc] initWithRootViewController:[[FSSubMitController alloc] init]];
   //设置modal的转场动画
    //翻页
//  nav.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:nav animated:YES completion:nil];
}
//切换子控制器
- (void)switchView:(titleViewBtnType)type{
    NSArray *arr =[NSMutableArray array];
    if (type == titleViewBtnTypeArticle) {
        //文章
        for (UIViewController *childVc in self.childViewControllers) {
            if ([childVc isKindOfClass:[FSArticleViewController class]]) {
                arr = @[@"热门",@"新上榜",@"七日热门",@"三十日热门",@"市集",@"有奖活动",@"日报"];
                [self switchController:childVc WithScrollerToolBar:arr];
                break;     }}}//专题
    else{
        for (UIViewController *childVc in self.childViewControllers) {
            if ([childVc isKindOfClass:[FSSpecialTopicViewController class]]) {
                arr = @[@"热门",@"推荐"];
                [self switchController:childVc WithScrollerToolBar:arr];
                break;      }}}
}

- (void)switchController:(UIViewController *)Vc WithScrollerToolBar:(NSArray *)arr{
    //先移除原本的控制器的View  和  toolbar
    [self.currentVc.view removeFromSuperview];
    [self.currentVc.scrollerToolBar removeFromSuperview];
    
    FSScrollerToolBar *scrollerTool = [FSScrollerToolBar ScrollerToolBarWithTitle:arr];
    scrollerTool.frame = CGRectMake(0, 64, ScreenWidth, 35);
    //默认选中第一个标签
    [scrollerTool selectBtnWithTitle:[arr firstObject]];
    [[UIApplication sharedApplication].keyWindow addSubview:scrollerTool];
    
    // 把对应子控制器的view添加上去
    Vc.view.frame = self.view.bounds;
    Vc.view.y += scrollerTool.height + CGRectGetMaxY(self.navigationController.navigationBar.frame);
    Vc.view.height -= CGRectGetMaxY(self.navigationController.navigationBar.frame) + scrollerTool.height + self.tabBarController.tabBar.height;
    [self.view addSubview:Vc.view];
    
    self.currentVc = (FSBaseTableController *)Vc;
    self.currentVc.scrollerToolBar = scrollerTool;
}

@end
